import { NzTreeNodeOptions } from "ng-zorro-antd/tree";
import { DeptDto } from "../models/sys/dept.dto";

export class TreeUtils {
    public static convertDeptToTreeNodes(depts: DeptDto[]): NzTreeNodeOptions[] {
        const deptNodes: NzTreeNodeOptions[] = [];

        for (const dept of depts) {

            const node = TreeUtils.convertDeptToTreeNode(dept);
            deptNodes.push(node);
        }

        return deptNodes;
    }

    private static convertDeptToTreeNode(dept: DeptDto): NzTreeNodeOptions {
        const result: NzTreeNodeOptions = {
            title: dept.deptName,
            key: dept.deptId.toString(),
        }

        let nodeChildren: NzTreeNodeOptions[] | undefined = undefined;
        if (dept.children) {
            nodeChildren = [];
            for (const child of dept.children) {
                const nodeChild = this.convertDeptToTreeNode(child);
                //            console.log(`add child ${nodeChild.key} to parent ${result.key}`);
                nodeChildren.push(nodeChild);
            }
            result.children = nodeChildren;
            result.isLeaf = false;
        } else {
 //           console.log(`dept ${dept.deptName} has no childre`);
            result.children = undefined;
            result.isLeaf = true;
        }

        // console.log(`add one tree with ${result.key}`);
        return result;
    }
}